#ifndef GETPERSPECTIVEMATRIX_H
#define GETPERSPECTIVEMATRIX_H

#include "wsymath/wsymath.h"
#include "wsymath/matrix3d.h"
#include <QMap>

void GetPerspectiveCameraMatrixNew(double fov, double aspect, double near1, double far1,
                             GLfloat *middleMatrix, GLfloat *perspectiveMatrix) {
    double t = tan(WSYMath::DegreeToRadian(fov / 2));
    double a = aspect;

    double l_near = near1, l_far = far1;

    double y_min = l_near * t, y_max = l_far * t;

    double y_mid = 2 / (1 / y_min + 1 / y_max);

    double fudgeFactor = y_mid / y_min - 1;
    //let fudgeFactor = 1 - y_mid / y_max;

    double l_mid = y_mid / t;

    //double z_m_minus = l_mid - l_near, z_m_plus = l_far - l_mid;

    Matrix3D::appendValuesDouble(middleMatrix,
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, -l_mid, 1
    );

    Matrix3D::appendValuesDouble(perspectiveMatrix,
        1 / a, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, t / fudgeFactor, t,
        0, 0, 0, y_mid
    );
}

QMap<QString, GLfloat*> *GetPerspectiveMatrix (double fov, double aspect, double near1, double far1) {
    GLfloat *middleMatrix = Matrix3D::newInstance();
    GLfloat *perspectiveMatrix = Matrix3D::newInstance();
    GetPerspectiveCameraMatrixNew(fov, aspect, near1, far1, middleMatrix, perspectiveMatrix);

    QMap<QString, GLfloat*> *returnMap = new QMap<QString, GLfloat*>();

    returnMap->insert("middleMatrix", middleMatrix);
    returnMap->insert("perspectiveMatrix", perspectiveMatrix);

    return returnMap;
}

#endif // GETPERSPECTIVEMATRIX_H
